﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using GodProject.peoples;
using WebGodServer.Database;

namespace WebGodService
{
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [ToolboxItem(false)]
  public class PeopleFindService : System.Web.Services.WebService
  {
    private readonly string DatabaseLocation =
      @"F:\\[VII]\\Губанов\\gubanov-arstan-mm-2011\\Lab6_1\\WebServices\\WebGodServer\\GodDatabase.sdf";

    private readonly DatabaseManager databaseManager;

    public PeopleFindService()
    {
      AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
      databaseManager = new DatabaseManager(DatabaseLocation);
    }

    [WebMethod]
    public List<ResponseData> GetPeoples(SearchRequest request)
    {
      SearchQuery query = ConvertFromServiceRequestToServerQuery(request);
      var result = databaseManager.Find(query);
      return ConvertFromServerResponseToServiceResponse(result);
    }

    private List<ResponseData> ConvertFromServerResponseToServiceResponse(
      IEnumerable<SearchResult> result)
    {
      List<ResponseData> data = new List<ResponseData>();
      foreach (SearchResult searchResult in result)
      {
        ResponseData responseItem = new ResponseData();
        FillServiceResponseData(responseItem, searchResult.Parent);
        FillServiceResponseData(responseItem, searchResult.Child);
        data.Add(responseItem);
      }
      return data;
    }

    private static void FillServiceResponseData(ResponseData responseItem, Student student)
    {
      responseItem.ChildAge = student.Age;
      responseItem.ChildName = student.Name;
      responseItem.ChildSurame = student.Middlename;
      responseItem.ChildSex = student.Sex == Sex.Female ? ResponseData.Sex.Female : ResponseData.Sex.Male;
      Botan botan = student as Botan;
      if (botan != null)
      {
        responseItem.ChildAverageMark = botan.AverageMark;
      }
    }

    private static void FillServiceResponseData(ResponseData responseItem, Parent parent)
    {
      responseItem.ParentAge = parent.Age;
      responseItem.ParentChildCount = parent.ChildCount;
      CoolParent coolParent = parent as CoolParent;
      if (coolParent != null)
      {
        responseItem.ParentMoney = coolParent.Money;
      }
      responseItem.ParentName = parent.Name;
    }

    private SearchQuery ConvertFromServiceRequestToServerQuery(SearchRequest request)
    {
      SearchQuery query =
        new SearchQuery()
          {
            AverageMark = ConvertToServerType(request.AvgMark),
            ChildAge = ConvertToServerType(request.ChildAge),
            ChildName = request.Name,
            ChildSex = ConvertToServerType(request.Sex),
            ParentAge = ConvertToServerType(request.ParentAge),
            ParentName = request.ParentName
          };

      return query;
    }

    private static SearchQuery.Interval<T> ConvertToServerType<T>(
      SearchRequest.Interval<T> from)
    {
      if (from == null)
      {
        return null;
      }
      return new SearchQuery.Interval<T>()
               {
                 From = from.From,
                 To = from.To
               };
    }

    private static SearchQuery.QuerySex ConvertToServerType(SearchRequest.Sexes sex)
    {
      switch (sex)
      {
        case SearchRequest.Sexes.All:
          return SearchQuery.QuerySex.All;
        case SearchRequest.Sexes.Female:
          return SearchQuery.QuerySex.Female;
        case SearchRequest.Sexes.Male:
          return SearchQuery.QuerySex.Male;
      }

      return SearchQuery.QuerySex.All;
    }
  }
}
